package org.atf.controller;

import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.atf.model.dao.PreferenceDAO;
import org.atf.model.entity.Preference;
import org.atf.utils.ATFUtils;



public class PreferenceController {
	PreferenceDAO preferenceDAO = new PreferenceDAO();
	
	public List<Preference> getPreferencesListByScope(String scope){	
		return preferenceDAO.getAllPreferenceByScope(scope);
	}
	
	public String addPreference(Preference preference){
		preferenceDAO.insertPreference(preference);
		return "OK";
	}
	
	public String updatePreference(Preference preference){
		preferenceDAO.updatePreference(preference);
		return "OK";
	}
	
	public String deletePreference(Integer id){
		preferenceDAO.deletePreference(id);
		return "OK";
	}
	
	public String addOrUpdateDefaultsPreference(){
		Preference pref=preferenceDAO.getPreferenceByName("");
		
		return "OK";
	}
	
	public String disableLDAP(){
		Preference pref=preferenceDAO.getPreferenceByNameScope("ENABLE_LDAP", "GLOBAL");
		if( pref != null){
			pref.setpValue("false");
			preferenceDAO.updatePreference(pref);
			return "OK";
		}else{
			return "ERROR: NOT FOUND";
		}
		
	}
	public String disableTAM(){
		Preference pref=preferenceDAO.getPreferenceByNameScope("TAM_ENABLED", "GLOBAL");
		if( pref != null){
			pref.setpValue("false");
			preferenceDAO.updatePreference(pref);
			return "OK";
		}else{
			return "ERROR: NOT FOUND";
		}
		
	}
	
	public String getPreferenceByNameScope(String name, String scope){
		Preference pref= preferenceDAO.getPreferenceByNameScope(name, scope);
		if(pref != null){
			return pref.getpValue();
		}else{
			return "";
		}
	}
	
	public String encryptPassword(String password){
		return ATFUtils.encode(password);
	}
	public String decryptPassword(String password){
		
		return ATFUtils.decode(password);
	}
}
